/* 验证身份证 */
const isCardNo = (num) => {
	if(num=='' || num==null || num==undefined){
		return false
	}
	num = num.toUpperCase();
	//身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。   
	if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
		return false;
	}
	//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
	//下面分别分析出生日期和校验位 
	var len, re;
	len = num.length;
	if (len == 15) {
		re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
		var arrSplit = num.match(re);

		//检查生日日期是否正确 
		var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]);
		var bCorrectDay;
		bCorrectDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[
				3])) &&
			(
				dtmBirth.getDate() == Number(arrSplit[4]));
		if (!bCorrectDay) {
			return false;
		} else {
			//将15位身份证转成18位 
			//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
			var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
			var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
			var nTemp = 0,
				i;
			num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);
			for (i = 0; i < 17; i++) {
				nTemp += num.substr(i, 1) * arrInt[i];
			}
			num += arrCh[nTemp % 11];
			return true;
		}
	}
	if (len == 18) {
		re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
		var arrSplit = num.match(re);

		//检查生日日期是否正确 
		var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
		var bCorrectDay;
		bCorrectDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(
				arrSplit[3])) &&
			(dtmBirth.getDate() == Number(arrSplit[4]));
		if (!bCorrectDay) {
			return false;
		} else {
			//检验18位身份证的校验码是否正确。 
			//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
			var valnum;
			var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
			var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
			var nTemp = 0,
				i;
			for (i = 0; i < 17; i++) {
				nTemp += num.substr(i, 1) * arrInt[i];
			}
			valnum = arrCh[nTemp % 11];
			if (valnum != num.substr(17, 1)) {
				return false;
			}
			return true;
		}
	}
	return false;
}

//获取最近4天的日期和礼拜天数
const initData = () => {
	const time = []
	const date = new Date()

	const now = date.getTime() //获取当前日期的时间戳
	let timeStr = 3600 * 24 * 1000 //一天的时间戳
	let obj = {
		0: "今天",
		1: "明天",
		2: "后天"
	}
	for (let i = 0; i < 4; i++) {
		const timeObj = {}
		var times = timeStamp(now + timeStr * i).date
		timeObj.val = getNowTime(new Date(times), "MM-dd") //保存日期
		timeObj.date = times //保存日期
		timeObj.timeStamp = now + timeStr * i //保存时间戳
		timeObj.week = obj[i] ?? timeStamp(now + timeStr * i).day
		time.push(timeObj)
	}
	return time
}


//时间戳转日期
const timeStamp = (time) => {
	const dates = new Date(time)
	const year = dates.getFullYear()
	const month = dates.getMonth() + 1
	const date = dates.getDate()
	const day = dates.getDay()
	const hour = dates.getHours()
	const min = dates.getMinutes()
	const days = ['日', '一', '二', '三', '四', '五', '六']
	return {
		allDate: `${year}/${strFormat(month)}/${strFormat(date)}`,
		date: `${strFormat(year)}-${strFormat(month)}-${strFormat(date)}`, //返回的日期 07-01
		day: `星期${days[day]}`, //返回的礼拜天数  星期一
		hour: strFormat(hour) + ':' + strFormat(min) + ':00' //返回的时钟 08:00
	}
}

//字符串拼接
const strFormat = (str) => {
	return str < 10 ? `0${str}` : str
}

/**
 * 日期加减
 */
const calculateDate = (oldDate, num) => {
	let time = new Date().setDate(oldDate.getDate() + parseInt(num));
	let date1 = new Date(time);
	return getNowTime(date1, 'yyyy-MM-dd');
}


/**
 * 格式化日期
 */
const getNowTime = (dateValue, fmt) => {
	var dateVal = new Date()
	if (dateValue != null) {
		var dateVal = dateomm(dateValue)
		dateVal = new Date(dateVal)
	}
	
	
	var o = {
		"M+": dateVal.getMonth() + 1, //月份 
		"d+": dateVal.getDate(), //日 
		"h+": dateVal.getHours(), //小时 
		"m+": dateVal.getMinutes(), //分 
		"s+": dateVal.getSeconds(), //秒 
		"q+": Math.floor((dateVal.getMonth() + 3) / 3), //季度 
		"S": dateVal.getMilliseconds() //毫秒 
	};
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (dateVal.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(fmt)) {
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k])
				.length)));
		}
	}
	// console.log(fmt)
	return fmt;
}

const dateomm = (date) => {
	var birth1 = new String(date);
	var applyTime = birth1.replace(/ \d+(:\d+){2}/, '')
	return applyTime;
}

// 传入参数：每间隔30分钟
const timeSlot = (step, e) => { //  step = 间隔的分钟  e = MM - dd
	var date = new Date(getNowTime(new Date(), "yyyy-") + e + " " + "00:00:00")

	var arr = [],
		timeArr = [];
	var slotNum = 24 * 60 / step // 算出多少个间隔
	for (var f = 0; f < slotNum; f++) { //  stepM * f = 24H*60M
		// arr.push(new Date(Number(date.getTime()) + Number(step*60*1000*f)))   //  标准时间数组
		var time = new Date(Number(date.getTime()) + Number(step * 60 * 1000 * f)) // 获取：零点的时间 + 每次递增的时间
		var hour = '',
			sec = '';
		var y = false;
		time.getHours() < 10 ? hour = '0' + time.getHours() : hour = time.getHours() // 获取小时
		time.getMinutes() < 10 ? sec = '0' + time.getMinutes() : sec = time.getMinutes() // 获取分钟

		//当前时间是否大于预约时间
		if (new Date() > new Date(getNowTime(new Date(), "yyyy-") + e + " " + hour + ":" + sec)) {
			y = true
		}

		timeArr.push({
			v: hour + ':' + sec,
			y: y,
			date: e
		})
	}
	return timeArr
}

const tuiJsonp = (url, callback, callbackname) => {
	// #ifdef H5
	window[callbackname] = callback;
	let tuiScript = document.createElement("script");
	tuiScript.src = url;
	tuiScript.type = "text/javascript";
	document.head.appendChild(tuiScript);
	document.head.removeChild(tuiScript);
	// #endif
}

//计算两点距离
const distance = (myLongLat,technicianInfo) => {
	var radLat1 = myLongLat.latitude * Math.PI / 180.0;
	var radLat2 = technicianInfo.latitude * Math.PI / 180.0;
	
	var a = radLat1 - radLat2;
	var b = myLongLat.longitude * Math.PI / 180.0 - technicianInfo.longitude * Math.PI / 180.0;
	
	var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math
		.pow(Math.sin(b / 2), 2)));
	s = s * 6378.137;
	
	s=Math.round(s * 10000) / 10;
	
	if(s < 1000)
	    s=s+"米"
	else if(s > 1000)
	    s=(Math.round(s/100)/10).toFixed(1) + "km"
	
	return s
}

//验证是否为空
const isEmpty = (v) => {
	if(v==""){
		return false
	} else if(v==null){
		return false
	} else if(v==undefined){
		return false
	}
	
	return true
}


export default {
	initData,
	timeStamp,
	timeSlot,
	tuiJsonp,
	distance,
	getNowTime,
	isCardNo,
	isEmpty,
	calculateDate
}
